package com.dzh.controller;

import com.dzh.pojo.Clazz;
import com.dzh.pojo.Student;
import com.dzh.pojo.StudentRes;
import com.dzh.service.ClassService;
import com.dzh.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    @Qualifier("StudentServiceImpl")
    private StudentService studentService;
    @Autowired
    @Qualifier("ClassServiceImpl")
    private ClassService classService;

    /**
     * 查找所有书籍
     * @param model
     * @return
     */
    @RequestMapping("/allStudent")
    public String list(Model model) {
        List<StudentRes> list = studentService.queryAllStudent();
        model.addAttribute("list", list);
        return "allStudent";
    }
    /**
     * 去添加书籍
     * @return
     */
    @RequestMapping("/toAddStudent")
    public String toAddPaper() {
        return "addStudent";
    }

    /**
     * 添加书籍
     * @param studentRes
     * @return
     */
    @RequestMapping("/addStudent")
    public String addPaper(StudentRes studentRes) {
        System.out.println(studentRes);
        Clazz clazz = new Clazz();
        clazz.setClassName(studentRes.getClassName());
        clazz.setYearName(studentRes.getYearName());
        int classId = classService.queryClassByName(clazz).getClassId();
        studentRes.setClassId(classId);
        studentService.addStudent(studentRes);
        return "redirect:/student/allStudent";
    }

    /**
     * 去更新书籍
     * @param model
     * @param sno
     * @return
     */
    @RequestMapping("/toUpdateStudent")
    public String toUpdateBill(Model model,@RequestParam("sno") int sno) {
        Student student = studentService.queryStudentById(sno);
        model.addAttribute("student",student );
        return "updateBill";
    }

    /**
     * 更新书籍
     * @param model
     * @param student
     * @return
     */
    @RequestMapping("/updateStudent")
    public String updateBill(Model model, Student student) {
        System.out.println(student);
        try{
            studentService.updateStudent(student);
        }catch (Exception e){
            e.printStackTrace();
        }
        Student student1 = studentService.queryStudentById(student.getSno());
        model.addAttribute("student", student1);
        return "redirect:/student/allStudent";
    }

    /**
     * 删除书籍
     * @param sno
     * @return
     */
    @RequestMapping("/del/{sno}")
    public String deleteBill(@PathVariable("sno") int sno) {
        studentService.deleteStudentById(sno);
        return "redirect:/student/allStudent";
    }


}